package com.outsourcing.bottle.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.outsourcing.bottle.domain.LoginUserInfoEntry;

/**
 * 瓶子类型表
 * 
 * @author 06peng
 * 
 */
public class LoginUserInfoTable {

	private final String TABLE_BOTTLE_TYPE = "loginuser_info";

	private final String LOGIN_UID = "login_uid";

	private DBManager db = null;

	public LoginUserInfoTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_TYPE)) {
			createBottleType();
		}
	}
	
	public LoginUserInfoTable(Context context) {
		if (db == null) {
			db = DBManager.get(context);
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_TYPE)) {
			createBottleType();
		}
	}

	private void createBottleType() {
		String createSql = "create table if not exists "
				+ TABLE_BOTTLE_TYPE
				+ " (id integer primary key autoincrement,"
				+ LOGIN_UID
				+ " integer, basicinfo_fullfill integer, basicinfo_tip varchar, avatar varchar,"
				+ "nickname varchar, doing varchar, facebookok integer, twitterok integer, qqok integer,"
				+ "sinaok integer, tqqok integer, renrenok integer, doubanok integer)";
		db.creatTable(db.getConnection(), createSql, TABLE_BOTTLE_TYPE);
	}

	public void saveLoginUserInfo(LoginUserInfoEntry loginUserInfo,
			int login_uid) {
		ContentValues values = new ContentValues();
		values.put(LOGIN_UID, login_uid);
		values.put("basicinfo_fullfill", loginUserInfo.getBasicinfo_fullfill());
		values.put("basicinfo_tip", loginUserInfo.getBasicinfo_tip());
		values.put("avatar", loginUserInfo.getAvatar());
		values.put("nickname", loginUserInfo.getNickname());
		values.put("doing", loginUserInfo.getDoing());
		values.put("facebookok", loginUserInfo.getFacebookok());
		values.put("twitterok", loginUserInfo.getTwitterok());
		values.put("qqok", loginUserInfo.getQqok());
		values.put("sinaok", loginUserInfo.getSinaok());
		values.put("tqqok", loginUserInfo.getTqqok());
		values.put("renrenok", loginUserInfo.getRenrenok());
		values.put("doubanok", loginUserInfo.getDoubanok());

		db.save(db.getConnection(), TABLE_BOTTLE_TYPE, values);
	}

	/**
	 * 根据ID删除
	 * */
	public void deleteLoginUserInfoById(int login_uid) {

		String deleteSql = LOGIN_UID + "=? ";
		db.delete(db.getConnection(), TABLE_BOTTLE_TYPE, deleteSql,
				new String[] { String.valueOf(login_uid) });
		db.closeConnection(db.getConnection());
	}

	public LoginUserInfoEntry getLoginUserInfo(int login_uid) {
		Cursor cursor = null;
		LoginUserInfoEntry loginUserInfo = null;
		try {
			cursor = db.find(db.getConnection(), "select * from "
					+ TABLE_BOTTLE_TYPE + " where " + LOGIN_UID + "=?",
					new String[] { String.valueOf(login_uid) });
			if (cursor != null) {
				while (cursor.moveToNext()) {
					loginUserInfo = new LoginUserInfoEntry();
					loginUserInfo.setBasicinfo_fullfill(cursor.getInt(cursor
							.getColumnIndex("basicinfo_fullfill")));
					loginUserInfo.setBasicinfo_tip((cursor.getString(cursor
							.getColumnIndex("basicinfo_tip"))));
					loginUserInfo.setAvatar((cursor.getString(cursor
							.getColumnIndex("avatar"))));
					loginUserInfo.setNickname((cursor.getString(cursor
							.getColumnIndex("nickname"))));
					loginUserInfo.setDoing((cursor.getString(cursor
							.getColumnIndex("doing"))));
					loginUserInfo.setFacebookok((cursor.getInt(cursor
							.getColumnIndex("facebookok"))));
					loginUserInfo.setTwitterok((cursor.getInt(cursor
							.getColumnIndex("twitterok"))));
					loginUserInfo.setQqok((cursor.getInt(cursor
							.getColumnIndex("qqok"))));
					loginUserInfo.setSinaok((cursor.getInt(cursor
							.getColumnIndex("sinaok"))));
					loginUserInfo.setTqqok((cursor.getInt(cursor
							.getColumnIndex("tqqok"))));
					loginUserInfo.setRenrenok((cursor.getInt(cursor
							.getColumnIndex("renrenok"))));
					loginUserInfo.setDoubanok((cursor.getInt(cursor
							.getColumnIndex("doubanok"))));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return loginUserInfo;
	}

	public void clearTable() {
		db.delete(db.getConnection(), TABLE_BOTTLE_TYPE, null, null);
	}
}
